package com.sam.im.heiqun.mvp.presenter;

import android.util.Log;
import android.view.View;

import com.sam.im.heiqun.entities.MyCircleItem;
import com.sam.im.heiqun.mvp.contract.CircleContract;
import com.sam.im.heiqun.mvp.modle.CircleModel;
import com.sam.im.heiqun.mvp.modle.CommentConfig;
import com.sam.im.heiqun.mvp.modle.CommentItem;
import com.sam.im.heiqun.mvp.modle.FavortItem;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.utils.circleutils.DatasUtil;
import com.sam.im.heiqun.utils.circleutils.IDataRequestListener;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @ClassName: CirclePresenter
 * @Description: 通知model请求服务器和通知view更新
 * @author yiw
 * @date 2015-12-28 下午4:06:03
 *
 */
public class CirclePresenter implements CircleContract.Presenter {
	private CircleModel circleModel;
	private CircleContract.View view;
	int pageNo = 1 ;
	public CirclePresenter(CircleContract.View view){
		circleModel = new CircleModel();
		this.view = view;
	}
	public void loadData(final int loadpage, String userid, final int type){//0 加载更多， 1  下拉刷新

		final List<MyCircleItem.ListBean> datas = new ArrayList<>();
		if (type==1){
			pageNo = 1 ;
		}else {
			pageNo++ ;
		}
		Log.i("info","pageNo=="+pageNo);
		PGService.getInstance().getcircle(pageNo+"",userid).subscribe(new AbsAPICallback<MyCircleItem>() {
			@Override
			protected void onResultError(ApiException ex) {
				if (ex.getCode() == 4){
//					datas.addAll(myCircleItem.getList());
					if(view!=null){
						view.update2loadData(loadpage,datas,type);
					}
				}
			}
			@Override
			public void onNext(MyCircleItem myCircleItem) {
				if (myCircleItem != null) {
					datas.addAll(myCircleItem.getList());
					if(view!=null){
						view.update2loadData(loadpage,datas,type);
					}
				}
			}
		});
		// List<CircleItem> datas = DatasUtil.createCircleDatas();
	}

	/**
	 *
	 * @Title: deleteCircle
	 * @Description: 删除动态
	 * @param  circleId
	 * @return void    返回类型
	 * @throws
	 */
	public void deleteCircle(final String circleId){
		if(view!=null){
			view.update2DeleteCircle(circleId);
		}


	}
	/**
	 *
	 * @Title: addFavort
	 * @Description: 点赞
	 * @param  circlePosition
	 * @return void    返回类型
	 * @throws
	 */
	public void addFavort(final int circlePosition){
		circleModel.addFavort(new IDataRequestListener() {
			@Override
			public void loadSuccess(Object object) {
				FavortItem item = DatasUtil.createCurUserFavortItem();


				if(view !=null ){
					view.update2AddFavorite(circlePosition, item);
				}

			}
		});
	}
	/**
	 *
	 * @Title: deleteFavort
	 * @Description: 取消点赞
	 * @param @param circlePosition
	 * @param @param favortId
	 * @return void    返回类型
	 * @throws
	 */
	public void deleteFavort(final int circlePosition){
		circleModel.deleteFavort(new IDataRequestListener() {

			@Override
			public void loadSuccess(Object object) {
				if(view !=null ){
					view.update2DeleteFavort(circlePosition);
				}
			}
		});
	}

	/**
	 *
	 * @Title: addComment
	 * @Description: 增加评论
	 * @param  content
	 * @param  config  CommentConfig
	 * @return void    返回类型
	 * @throws
	 */
	public void addComment(final String content, final CommentConfig config){
		if(config == null){
			return;
		}
		circleModel.addComment(new IDataRequestListener() {

			@Override
			public void loadSuccess(Object object) {
				CommentItem newItem = null;
				if (config.commentType == CommentConfig.Type.PUBLIC) {
					newItem = DatasUtil.createPublicComment(content);
				} else if (config.commentType == CommentConfig.Type.REPLY) {
					newItem = DatasUtil.createReplyComment(config.replyUser, content);
				}
				if(view!=null){
					view.update2AddComment(config.circlePosition, newItem,config);
				}
			}

		});
	}

	/**
	 *
	 * @Title: deleteComment
	 * @Description: 删除评论
	 * @param @param circlePosition
	 * @param @param commentId
	 * @return void    返回类型
	 * @throws
	 */
	public void deleteComment(final int circlePosition, final String commentId){
		circleModel.deleteComment(new IDataRequestListener(){

			@Override
			public void loadSuccess(Object object) {
				if(view!=null){
					view.update2DeleteComment(circlePosition, commentId);
				}
			}

		});
	}

	/**
	 *
	 * @param commentConfig
	 */
	public void showEditTextBody(CommentConfig commentConfig){
		if(view != null){
			view.updateEditTextBodyVisible(View.VISIBLE, commentConfig);
		}
	}

	/**
	 * 显示礼物列表
	 * @param commentConfig
	 */
	public void showGiftSelecter(CommentConfig commentConfig){
		if(view != null){
			view.showGiftSelecter(View.VISIBLE, commentConfig);
		}
	}


	/**
	 * 清除对外部对象的引用，反正内存泄露。
	 */
	public void recycle(){
		this.view = null;
	}
}
